package com.lineage.server.datatables;

import com.lineage.DatabaseFactory;
import com.lineage.server.model.L1WeaponSkill;
import com.lineage.server.utils.PerformanceTimer;
import com.lineage.server.utils.SQLUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class WeaponSkillTable {
  private static final Log _log = LogFactory.getLog(WeaponSkillTable.class);
  
  private static WeaponSkillTable _instance;
  
  private static final Map<Integer, L1WeaponSkill> _weaponIdIndex = new HashMap<>();
  
  public static WeaponSkillTable get() {
    if (_instance == null)
      _instance = new WeaponSkillTable(); 
    return _instance;
  }
  
  public void load() {
    PerformanceTimer timer = new PerformanceTimer();
    Connection con = null;
    PreparedStatement pstm = null;
    ResultSet rs = null;
    try {
      con = DatabaseFactory.get().getConnection();
      pstm = con.prepareStatement("SELECT * FROM `weapon_skill`");
      rs = pstm.executeQuery();
      fillWeaponSkillTable(rs);
    } catch (SQLException e) {
      _log.error(e.getLocalizedMessage(), e);
    } finally {
      SQLUtil.close(rs);
      SQLUtil.close(pstm);
      SQLUtil.close(con);
    } 
    _log.info("载入技能武器资料数量: " + _weaponIdIndex.size() + "(" + timer.get() + "ms)");
  }
  
  private void fillWeaponSkillTable(ResultSet rs) throws SQLException {
    while (rs.next()) {
      int weaponId = rs.getInt("weapon_id");
      int probability = rs.getInt("probability");
      int fixDamage = rs.getInt("fix_damage");
      int randomDamage = rs.getInt("random_damage");
      int area = rs.getInt("area");
      int skillId = rs.getInt("skill_id");
      int skillTime = rs.getInt("skill_time");
      int effectId = rs.getInt("显示特效");
      int effectTarget = rs.getInt("effect_target");
      boolean isArrowType = rs.getBoolean("arrow_type");
      int attr = rs.getInt("attr");
      int gfxId = rs.getInt("显示特效_1");
      int gfxId2 = rs.getInt("显示特效_2");
      int gfxId3 = rs.getInt("显示特效_3");
      int gfxId4 = rs.getInt("显示特效_4");
      int gfxId5 = rs.getInt("显示特效_5");
      double x_str = rs.getDouble("x_str");
      double x_dex = rs.getDouble("x_dex");
      double x_int = rs.getDouble("x_int");
      double x_sp = rs.getDouble("x_sp");
      int c_hp = rs.getInt("c_hp");
      int c_mp = rs.getInt("c_mp");
      int weapon_lv = rs.getInt("weapon_lv");
      int weapon_addpro = rs.getInt("weapon_addpro");
      L1WeaponSkill weaponSkill = new L1WeaponSkill(weaponId, probability, fixDamage, randomDamage, area, 
          skillId, skillTime, effectId, effectTarget, isArrowType, attr, gfxId, gfxId2, gfxId3, gfxId4, 
          gfxId5, c_hp, c_mp, x_str, x_dex, x_int, x_sp, weapon_lv, weapon_addpro);
      _weaponIdIndex.put(Integer.valueOf(weaponId), weaponSkill);
    } 
  }
  
  public L1WeaponSkill getTemplate(int weaponId) {
    return _weaponIdIndex.get(Integer.valueOf(weaponId));
  }
}
